<template>
    <div>
      <div style="display: flex">
        <div style="width: 50%">
          <el-collapse v-model="activeName" accordion>
            <el-collapse-item v-for="x in data" :key="x.id" :title="x.name" :name="x.id">
              <div>{{ x.content }}</div>
            </el-collapse-item>
          </el-collapse>
        </div>
        <div style="flex: 1; margin-left: 50px; margin-top: 50px">
          <div id="bie" style="width: 100%; height: 400px"> </div>
        </div>
      </div>

      <div style="display: flex">
        <div id="bar" style="width: 100%; height: 400px"> </div>
        <div id="line" style="width: 100%; height: 400px"> </div>
      </div>

      <div>
        <div style="margin-top: 40px">
          <div style="font-size: 18px">欢迎进入移民管理系统首页🐱🦊🐹🐸🐥</div>
          <div style="font-size: 18px; margin: 15px 0;">欢迎进入移民管理系统首页︎⚽️⚽️⚽️⚽️⚽️</div>
          <div style="font-size: 18px">欢迎进入移民管理系统首页🚗🚗🚗🚗🏎️</div>
        </div>
      </div>
    </div>
</template>

<script>


import request from "@/utils/request";
import * as echarts from "echarts";
export default {
  name: 'HomeView',
  data(){
    return {
      activeName: '1',
      data: [],
      bieData: [],
    }
  },
  mounted() {
    this.findNotice();
    this.initEcharts();  //初始化echarts图，所有的接口请求都可以在这个方法里完成。
  },
  methods: {
    findNotice(){
      request.get('/notice').then(res => {
        if (res.code === '0'){
          this.data = res.data
          this.activeName = res.data[0].id
        }else {
          this.$message.error(res.msg);
        }
      })
    },

    // 饼图请求后端数据接口
    initEcharts(){
        //请求饼图
        request.get('/book/echarts/bie').then(res => {
          if (res.code === '0'){
            this.initBie(res.data);
          }else {
            this.$message.error(res.msg);
          }
        })


      //请求柱状图和折线图
      request.get('/book/echarts/bar').then(res => {
        if (res.code === '0'){
          // console.log(res.data)
          this.initBar(res.data.xAxis,res.data.yAxis);//柱状图
          this.initLine(res.data.xAxis,res.data.yAxis);//折线图
        }else {
          this.$message.error(res.msg);
        }
      })
    },

    initBie(data){
      let chartDom = document.getElementById('bie');
      let myChart = echarts.init(chartDom);
      let option;

    option = {
      title: {
        text: '图书统计（饼图）',
        subtext: '统计维度：图书分类',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: data,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    option && myChart.setOption(option);
    },

    initBar(xAxis,yAxis){
      let chartDom = document.getElementById('bar');
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        title: {
          text: '图书统计(柱状图)',
          subtext: '统计维度：图书分类',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: yAxis,
            type: 'bar'
          }
        ]
      };

      option && myChart.setOption(option);
    },

    initLine(xAxis,yAxis){
      let chartDom = document.getElementById('line');
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        title: {
          text: '图书统计(柱状图)',
          subtext: '统计维度：图书分类',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: yAxis,
            type: 'line',
            smooth: true
          }
        ]
      };

      option && myChart.setOption(option);
    }

  }
}
</script>
